brute force greedy math

Please click on ads to support us..

Python Code:

import os
import sys
import math
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
import heapq
import itertools

input = sys.stdin.readline


def multiple():
    a = map(int, input().split())
    return a


def array():
    a = input().split()
    return a


def intarray():
    a = list(map(int, input().split()))
    return a


def intinput():
    n = int(input())
    return n


def strinput():
    s = input().strip()
    return s


def isPrime(n):
    val = int(math.sqrt(n)) + 1
    for i in range(2, val):
        if n % i == 0:
            return False
    return True


def power(x, y, p):
    res = 1
    x = x % p

    if (x == 0):
        return 0

    while (y > 0):
        if ((y & 1) == 1):
            res = (res * x) % p

        y = y >> 1
        x = (x * x) % p

    return res


def modexp(x, n, m):
    if (n == 0):
        return 1

    elif (n % 2 == 0):
        return modexp((x * x) % m, n // 2, m)

    else:
        return (x * modexp((x * x) % m,
                           (n - 1) / 2, m) % m)


def getFractionModulo(a, b, m):
    c = math.gcd(a, b)

    a = a // c
    b = b // c

    d = modexp(b, m - 2, m)

    ans = ((a % m) * (d % m)) % m

    return ans


def factorial(n, dp):
    if dp[n] != 0:
        return dp[n]
    val = 1
    for i in range(2, n + 1):
        val *= i
        dp[i] = val
    return val


def notmultiple(a, b):
    while a % b == 0:
        a = a // b
    if a == 1:
        return False
    return True

def median(a):
    n = len(a)
    if n%2 == 0:
        val1 = n//2
        val2 = val1-1
        return (a[val1] + a[val2])/2
    else:
        val1 = n//2
        return a[val1]

def solution():
            n = intinput()
    s = strinput()
    a = [0]*(n+1)
    for i in range(1,n+1):
        if s[i-1] == '1':
            a[i] = 1
    ans = 0
    cost = [0]*(n+1)
    for i in range(n,0,-1):
        for j in range(i,n+1,i):
            if a[j] == 1:
                break
            cost[j] = i
    for i in range(1,n+1):
        if a[i] != 1:
            ans += cost[i]
    print(ans)
    return

t = 1
t = int(input())
for _ in range(t):
            solution()

C++ Code:

#include<cstdio>
char s[1000007];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
long long ans=0;
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;++i)
for(int j=1;i*j<=n&&s[i*j]!=49;++j)
ans+=i*(s[i*j]==48),s[i*j]=50;
printf("%lld\n",ans);
}
return 0;
}


Comments

Submit
0 Comments
More Questions

1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal